<template>
  <div class="manager-container">
    <!--  头部  -->
    <div class="manager-header">
      <div class="manager-header-left">
        <img src="@/assets/imgs/logo.png" />
        <div class="title">你的自习室平台</div>
      </div>

      <div class="manager-header-center">
        <el-breadcrumb separator-class="el-icon-arrow-right">
          <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
          <el-breadcrumb-item :to="{ path: $route.path }">{{ $route.meta.name }}</el-breadcrumb-item>
        </el-breadcrumb>
      </div>

      <div class="manager-header-right">
        <el-dropdown placement="bottom">
          <div class="avatar">
            <img :src="user.avatar || 'https://cube.elemecdn.com/3/7c/3ea6beec64369c2642b92c6726f1epng.png'" />
            <div>{{ user.name ||  '管理员' }}</div>
          </div>
          <el-dropdown-menu slot="dropdown">
            <el-dropdown-item @click.native="goToPerson">个人信息</el-dropdown-item>
            <el-dropdown-item @click.native="$router.push('/password')">修改密码</el-dropdown-item>
            <el-dropdown-item @click.native="logout">退出登录</el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown>
      </div>
    </div>

    <!--  主体  -->
    <div class="manager-main">
      <!--  侧边栏  -->
      <div class="manager-main-left">
        <el-menu
            :default-openeds="['info', 'user', 'card', 'grid', 'system']"
        router
        style="border: none"
        :default-active="$route.path"
        >
        <!-- 1. 首页入口（独立项） -->
        <el-menu-item index="/home">
          <i class="el-icon-s-home"></i>
          <span slot="title">系统首页</span>
        </el-menu-item>

        <el-submenu index="info">
          <template slot="title">
            <i class="el-icon-menu"></i><span>信息管理</span>
          </template>
          <el-menu-item index="/notice">公告信息</el-menu-item>
          <el-menu-item index="/sysRoom">自习室数据</el-menu-item>
          <el-menu-item index="/clock">打卡记录</el-menu-item>
          <el-menu-item index="/sysValue">系统数据</el-menu-item>
        </el-submenu>

        <el-submenu index="user">
          <template slot="title">
            <i class="el-icon-user"></i><span>用户管理</span>
          </template>
          <el-menu-item index="/admin">管理员信息</el-menu-item>
          <el-menu-item index="/user">用户信息</el-menu-item>
          <el-menu-item index="/ai">用户与AI聊天记录信息</el-menu-item>
        </el-submenu>

        <!-- 4. 会员卡全流程分组（卡片、订单、使用记录） -->
        <el-submenu index="card">
          <template slot="title">
            <i class="el-icon-credit-card"></i><span>商品管理</span>  <!-- 图标替换为卡片相关 -->
          </template>
          <el-menu-item index="/card">会员卡信息</el-menu-item>
          <el-menu-item index="/book">资料信息</el-menu-item>
          <el-menu-item index="/cardOrder">订单信息</el-menu-item>
          <el-menu-item index="/cardUse">卡的使用记录</el-menu-item>
        </el-submenu>

        <!-- 5. 网格（座位）管理分组（网格基础、预定、类型） -->
        <el-submenu index="grid">
          <template slot="title">
            <i class="el-icon-s-grid"></i><span>网格座位管理</span>  <!-- 图标替换为网格相关 -->
          </template>
          <el-menu-item index="/grid">网格情况</el-menu-item>
          <el-menu-item index="/gridReservation">位置预定</el-menu-item>
          <el-menu-item index="/gridType">网格类型</el-menu-item>
        </el-submenu>
        </el-menu>
      </div>

      <!--  数据表格  -->
      <div class="manager-main-right">
        <router-view @update:user="updateUser" />
      </div>
    </div>

  </div>
</template>

<script>
export default {
  name: "Manager",
  data() {
    return {
      user: JSON.parse(localStorage.getItem('xm-user') || '{}'),
    }
  },
  created() {
    if (!this.user.id) {
      this.$router.push('/login')
    }
  },
  methods: {
    updateUser() {
      this.user = JSON.parse(localStorage.getItem('xm-user') || '{}')   // 重新获取下用户的最新信息
    },
    goToPerson() {
      if (this.user.role === 'ADMIN') {
        this.$router.push('/adminPerson')
      }
    },
    logout() {
      localStorage.removeItem('xm-user')
      this.$router.push('/login')
    }
  }
}
</script>

<style scoped>
@import "@/assets/css/manager.css";
</style>